﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Collections.Generic;
using Catalyst.Web.DynamicData;

namespace AdventureWorks.DynamicData.FilterTemplates
{
    public partial class ForeignKey : Catalyst.Web.DynamicData.EqualsFilterUserControlBase
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            PopulateListControl(DropDownList1);
        }

        public override string Value
        {
            get
            {
                return DropDownList1.SelectedValue;
            }
            set
            {
                DropDownList1.SelectedIndex = -1;
                ListItem li = DropDownList1.Items.FindByValue(value);
                if (li != null)
                    li.Selected = true;
            }
        }

        public override TypeCode ValueType
        {
            get { return ForeignKeyColumn.ParentTable.PrimaryKeyColumns[0].TypeCode; }
        }

        public override IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource)
        {
            yield return new EqualsExpressionParameter()
            {
                Name = string.Format("{0}.{1}", Column.Name, ForeignKeyColumn.ParentTable.PrimaryKeyColumns[0].Name),
                ValueType = ValueType,
                Value = Value
            };
        }
    }
}